package com.helen.ui.donation;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helen.adapter.DonationListAdapter;
import com.helen.entity.DonationListEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.MyLog;
import com.helen.utils.PhoneUtils;
import com.helen.utils.tiputils.MToast;
import com.helen.widget.SearchEditText;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.cache.converter.SerializableDiskConverter;
import com.zhouyou.http.cache.model.CacheMode;
import com.zhouyou.http.cache.model.CacheResult;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2018/5/4.
 * 捐赠列表
 */

public class DonationActivity extends BaseActivity {
    @BindView(R.id.et_search)
    SearchEditText etSearch;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    private View errorView;
    private List<DonationListEntity> list = new ArrayList();
    private DonationListAdapter donationListAdapter;
    private int page = 1;
    private String title;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_donation_list;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(getResources().getStringArray(R.array.home_classification)[5]);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvList.getParent(), false);
        donationListAdapter = new DonationListAdapter(R.layout.item_donation_list, list);
        donationListAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        swipeRefresh.setRefreshing(true);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.appThemeColor));
        rvList.setLayoutManager(new LinearLayoutManager(DonationActivity.this));
        rvList.setAdapter(donationListAdapter);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        donationListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, rvList);
        donationListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                DonationListEntity donationListEntity = (DonationListEntity) adapter.getData().get(position);
                Intent ti = new Intent(DonationActivity.this, DonationDetailActivity.class);
                ti.putExtra("donationListEntity", donationListEntity);
                startActivity(ti);
            }
        });


        etSearch.setOnSearchClickListener(new SearchEditText.OnSearchClickListener() {
            @Override
            public void onSearchClick(View view) {
                title = etSearch.getText() + "";
                page = 1;
                refresh();
            }
        });

        etSearch.setOnClearClickListener(new SearchEditText.OnClearClickListener() {
            @Override
            public void onClearClick(View view) {
                title = "";
                page = 1;
                refresh();
            }
        });
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                page = 1;
                title = s + "";
            }
        });


        refresh();
    }

    @Override
    protected void updateViews() {

    }

    private void refresh() {
        page = 1;
        donationListApi();
    }


    private void loadMore() {
        page++;
        donationListApi();
    }


    private void donationListApi() {
        HttpParams params = new HttpParams();
        params.put("title", title);
        params.put("page", page + "");
        params.put("size", "10");
        MyLog.e("yang", "params==" + params.toString());
        mMProgressDialog.show();
        EasyHttp.post(Constants.API_JSONAPI_DONATIONLIST)
                .cacheMode(CacheMode.CACHEANDREMOTEDISTINCT)
                .cacheKey(Constants.API_JSONAPI_DONATIONLIST)
                .cacheDiskConverter(new SerializableDiskConverter())
                .execute(new SimpleCallBack<CacheResult<String>>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "爱心捐赠列表失败==" + e.toString());
                        MToast.makeTextShort(DonationActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(CacheResult<String> stringCacheResult) {
                        String data = PhoneUtils.checkResponseFlag2(DonationActivity.this, stringCacheResult.data, TAG);
                        if (data != null) {
                            list = new Gson().fromJson(data, new TypeToken<List<DonationListEntity>>() {
                            }.getType());
                            if (page == 1) {
                                donationListAdapter.setNewData(list);
                            } else {
                                donationListAdapter.addData(list);
                            }
                            if (list.size() < 10) {
                                donationListAdapter.loadMoreEnd(false);
                                donationListAdapter.setEmptyView(errorView);
                            } else {
                                donationListAdapter.loadMoreComplete();
                            }
                        }
                    }
                });
    }


}
